<?php
require_once("../data/common.php");

$device_id = 4;

$sql = "SELECT id, time_received FROM gps_data WHERE device_id = ? ORDER BY time_received;";
$stmt = $dbh->prepare($sql);
$stmt->execute(array($device_id));

$rows = $stmt->fetchAll();

$last_time = 0;
$split_time = 1*60*10; // in seconds
$path_id = 0;

foreach ($rows as $rs) {
	$time = strtotime($rs["time_received"]);
	$id = $rs["id"];
	
	if ( ($time - $last_time) > 10 ) {
		$path_id++;
	}

	$tracks["$path_id"]["device_id"] = $device_id;
	if (!isset($tracks["$path_id"]["time_received"])) $tracks["$path_id"]["time_received"] = $rs["time_received"];
	if (!isset($tracks["$path_id"]["first_id"])) $tracks["$path_id"]["first_id"] = $id;
	if (!isset($tracks["$path_id"]["count"])) $tracks["$path_id"]["count"] = 0;
	$tracks["$path_id"]["last_id"] = $id;
	$tracks["$path_id"]["count"] += 1;

	$last_time = $time;
}

while (list ($id, $data) = each ($tracks) ) {
	if ($data["count"] > 10) printf("INSERT INTO tracks (device_id,first_id,last_id,created) VALUES (%d, %d, %d, '%s');\n", $data["device_id"], $data["first_id"], $data["last_id"], $data["time_received"]);

}

?>
